package com.example.dicos.service.impl;

import com.example.dicos.entity.WxRole;
import com.example.dicos.entity.WxUserRoleRelation;
import com.example.dicos.entity.dot.WxUserRoleRelationDto;
import com.example.dicos.entity.vo.WxRoleVo;
import com.example.dicos.entity.vo.WxRoleVos;
import com.example.dicos.entity.vo.WxUserRoleRelationVo;
import com.example.dicos.mapper.WxRoleMapper;
import com.example.dicos.mapper.WxUserRoleRelationMapper;
import com.example.dicos.service.WxRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author 开发者
 * @since 2022-07-11
 */
@Service
public class WxRoleServiceImpl implements WxRoleService {

    @Autowired
    private WxRoleMapper wxRoleMapper;
    @Autowired
    private WxUserRoleRelationMapper wxUserRoleRelationMapper;

    /**
     * 查询角色列表
     *
     * @return
     */
    @Override
    public List<WxRole> queryWxRoleList() {
        return wxRoleMapper.queryWxRoleList();
    }

    /**
     * 用户分配角色
     *
     * @param wxUserRoleRelationDto
     * @return
     * @throws Exception
     */
    @Override
    public Integer addUserRole(WxUserRoleRelationDto wxUserRoleRelationDto) throws Exception {
        WxUserRoleRelation wxUserRoleRelation = new WxUserRoleRelation();
        String[] roleId = wxUserRoleRelationDto.getRoleId();
        if (roleId.length == 0) {
            throw new Exception("分配角色不能为空");
        }
        //存在 先删除在添加
        List<WxUserRoleRelationVo> wxUserRoleRelationVos = wxRoleMapper.uertRole(wxUserRoleRelationDto.getAdminId());
        if (wxUserRoleRelationVos.size() > 0) {
            wxUserRoleRelationVos.stream().forEach(del -> {
                wxRoleMapper.uertRoleDelete(del.getAdminId());
            });
        }
        List<String> list = Arrays.asList(roleId);
        BeanUtils.copyProperties(wxUserRoleRelationDto, wxUserRoleRelation);
        list.stream().forEach(s -> {
            wxUserRoleRelation.setRoleId(s);
            wxUserRoleRelationMapper.insert(wxUserRoleRelation);
        });
//        String join = StringUtils.join(",", Arrays.asList(roleId));
//        wxUserRoleRelation.setRoleId(join);
        return 1;
    }

    /**
     * 根据用户id查询出对应的角色
     *
     * @param userId
     * @return
     */
    @Override
    public List<WxRoleVo> queryWxRoleVoList(Long userId) {
        List<WxRoleVo> list = new ArrayList<>();
        List<WxRoleVos> wxRoleVos = wxRoleMapper.queryWxRoleVoList(userId);
        wxRoleVos.stream().forEach(wxRoleVo -> {
            WxRoleVo wxRole = new WxRoleVo();
//            String roleId = wxRoleVo.getRoleId();
//            String[] split = roleId.split(",");
//            wxRole.setRoleId(split);
            BeanUtils.copyProperties(wxRoleVo, wxRole);
            list.add(wxRole);
        });
        return list;
    }

}
